//
//  RBAlertVC.m
//  RBDemoProj
//
//  Created by Lan on 2019/2/13.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBAlertVC.h"
#import "UIButton+RBAdd.h"
#import <Masonry.h>
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "RBThemeConfig.h"
#import "RBLongTextView.h"
#import "UIViewController+RBAdd.h"

@interface RBAlertVC ()

@property(nonatomic, strong) UILabel *titleL;
@property(nonatomic, strong) RBLongTextView *textView;
@property(nonatomic, strong) UIButton *cancelBtn;
@property(nonatomic, strong) UIButton *confirmBtn;

@property(nonatomic, copy) void(^block)(NSInteger index);

@end

@implementation RBAlertVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
}

#pragma mark - Private

- (void)dismiss
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)show
{
    [self.contentView addLittleZoomAnimation];
}

- (UIColor *)themeColor
{
    return [RBThemeConfig themeColor];
}

#pragma mark - Public

- (void)showFromVc:(UIViewController *)vc title:(NSString *)title content:(NSString *)content buttons:(NSArray *)buttons selectBlock:(void (^)(NSInteger))block
{
    [vc presentVc:self];
    
    self.contentView = self.contentView? self.contentView: ({
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor whiteColor];
        [view rbSetCornerRadius:8];
        [self.view addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        view;
    });
    self.titleL.text = title;
    self.textView.text = content;
    if (buttons.count < 2)
    {
        [self.cancelBtn rbSetTitle:buttons.firstObject];
        self.confirmBtn.hidden = YES;
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-15);
            make.width.mas_equalTo(87);
            make.height.mas_equalTo(30);
            make.centerX.mas_equalTo(0);
        }];
    }
    else
    {
        [self.cancelBtn rbSetTitle:buttons.firstObject];
        [self.confirmBtn rbSetTitle:buttons.lastObject];
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.bottom.mas_equalTo(-15);
            make.width.mas_equalTo(87);
            make.height.mas_equalTo(30);
        }];
        [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.bottom.mas_equalTo(-15);
            make.width.mas_equalTo(87);
            make.height.mas_equalTo(30);
        }];
    }
    self.block = block;
}

#pragma mark - Event

- (void)confirmBtnTapped:(id)sender
{
    if (self.block)
    {
        self.block(1);
    }
    [self dismiss];
}

- (void)cancelBtnTapped:(id)sender
{
    if (self.block)
    {
        self.block(0);
    }
    [self dismiss];
}

#pragma mark - Getter

- (UILabel *)titleL
{
    if (!_titleL)
    {
        self.titleL = ({
            UILabel *view = [UILabel new];
            view.textColor = [UIColor whiteColor];
            view.backgroundColor = [self themeColor];
            view.font = [UIFont systemFontOfSize:15];
            view.numberOfLines = 1;
            view.textAlignment = NSTextAlignmentCenter;
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(37);
            }];
            view;
        });
    }
    return _titleL;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn)
    {
        self.cancelBtn = ({
            UIButton *btn = [UIButton new];
            [btn rbSetCornerRadius:15];
            [btn rbSetTitleColor:[self themeColor]];
            [btn rbSetBorderWitdh:1 borderColor:[self themeColor]];
            [btn rbSetFontSize:15];
            [btn rbAddTarget:self action:@selector(cancelBtnTapped:)];
            [self.contentView addSubview:btn];
            btn;
        });
    }
    return _cancelBtn;
}

- (UIButton *)confirmBtn
{
    if (!_confirmBtn)
    {
        self.confirmBtn = ({
            UIButton *btn = [UIButton new];
            [btn rbSetCornerRadius:15];
            [btn rbSetTitleColor:[UIColor whiteColor]];
            [btn setBackgroundColor:[self themeColor]];
            [btn rbSetFontSize:15];
            [btn rbAddTarget:self action:@selector(confirmBtnTapped:)];
            [self.contentView addSubview:btn];
            btn;
        });
    }
    return _confirmBtn;
}

- (RBLongTextView *)textView
{
    if (!_textView)
    {
        self.textView = ({
            RBLongTextView *view = [RBLongTextView new];
            view.minHeight = 50;
            view.maxHeight = kRBMainScreenHeight - kRBStatusBarHeight - kRBSafeBottomHeight - 200;
            view.consWidth = 259*kRBFactor;
            view.font = [UIFont systemFontOfSize:13];
            view.lineSpacing = 7;
            view.textColor = [RBThemeConfig textBlackColor];
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.titleL.mas_bottom).offset(15);
                make.left.mas_equalTo(15);
                make.height.mas_equalTo(50);
                make.right.mas_equalTo(-15);
                make.bottom.mas_equalTo(self.cancelBtn.mas_top).offset(-15);
                make.width.mas_equalTo(view.consWidth);
            }];
            view;
        });
    }
    return _textView;
}

@end
